package cn.itcast.mq.listen;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author mrchen
 * @date 2022/6/14 15:05
 */
@Component
public class SpringRabbitListener {
    // rabbit监听队列的注解    监听的队列名称
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMsg(String msg){ // 入参: 消息
//        System.out.println("接收到simple.queue中消息:  " + msg);
//    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMsg1(String msg) throws InterruptedException { // 入参: 消息
        System.out.println("消费者1 消费消息:  " + msg);
        Thread.sleep(25);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMsg2(String msg) throws InterruptedException { // 入参: 消息
        System.err.println("消费者2 消费消息:  " + msg);
        Thread.sleep(100);
    }
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueueMsg1(String msg) throws InterruptedException { // 入参: 消息
        System.out.println("fanout.queue1 消费消息:  " + msg);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueueMsg2(String msg) throws InterruptedException { // 入参: 消息
        System.err.println("fanout.queue2 消费消息:  " + msg);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue1"),  // 要声明的队列
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT),  //  要声明的交换机
            key = {"blue","red"}            // 绑定routeKey
    ))
    public void listenDirectQueueMsg1(String msg) throws InterruptedException { // 入参: 消息
        System.out.println("direct.queue1 消费消息:  " + msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue2"),  // 要声明的队列
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT),  //  要声明的交换机
            key = {"yellow","red"}            // 绑定routeKey
    ))
    public void listenDirectQueueMsg2(String msg) throws InterruptedException { // 入参: 消息
        System.err.println("fanout.queue2 消费消息:  " + msg);
    }



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue1"),
            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueueMsg1(String msg) throws InterruptedException { // 入参: 消息
        System.out.println("topic.queue1  路由: china.#  消费消息:  " + msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("topic.queue2"),
            exchange = @Exchange(value = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueueMsg2(String msg) throws InterruptedException { // 入参: 消息
        System.err.println("topic.queue2 消费消息:  " + msg);
    }
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueueMsg2(Map map) throws InterruptedException { // 入参: 消息
        System.err.println("object.queue 消费消息:  " + map);
    }
}
